package com.jiuhong.mytvapplication.floatwindow;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;

/**
 * Author by LY
 * Email 1078410438@qq.com
 * Date on 2022/3/7.
 */
public class FloatWindowBuilder {

    private FloatWindowBuilder instance;
    private Context mContext;
    private WindowManager windowManager;

    public FloatWindowBuilder(MyBuild myBuild) {
        this.mContext = myBuild.mContext;
        this.windowManager = myBuild.mWindowManager;
    }

    public WindowManager getWindowManager() {
        return this.windowManager;
    }

    static final class MyBuild {
        private Context mContext;
        private WindowManager mWindowManager;
        private WindowManager.LayoutParams layoutParams;


        public MyBuild setContext(Context context) {
            mContext = context;
            initWindow();
            return this;
        }

        private void initWindow() {
            mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            layoutParams = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            }
            layoutParams.format = PixelFormat.RGBA_8888;
            layoutParams.gravity = Gravity.LEFT | Gravity.RIGHT;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            Display defaultDisplay = mWindowManager.getDefaultDisplay();
            int height = defaultDisplay.getHeight();
            int width = defaultDisplay.getWidth();
            layoutParams.x = 0;
            layoutParams.y = 0;
        }

        public MyBuild setGravity(int a, int b) {
            layoutParams.gravity = a | b;
            return this;
        }

        public MyBuild setFlags(int... flags) {
            for (int flag : flags) {
                layoutParams.flags |= flag;
            }
            return this;
        }

        public MyBuild setSize(int width, int height) {
            layoutParams.width = width;
            layoutParams.height = height;
            return this;
        }

        public MyBuild setPosition(int x, int y) {
            layoutParams.x = x;
            layoutParams.y = y;
            return this;
        }

        public FloatWindowBuilder build() {
            return new FloatWindowBuilder(this);
        }
    }

}
